﻿/*
 Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.utils{
	import flash.utils.getDefinitionByName;

	/**
	     * 
	     * Returns the class Object based on the String name 
	     * representation
	     * 
	     * @see flash.utils.getDefinitionByName
	     * 
	     */
	public class Reflection {
		/**
		         *
		         * Returns the class Object for the String name representation 
		         * specified in the type parameter.
		         *
		         * <p>
		         * Keep in mind that you will always need an internal reference 
		         * to the class that is passed to <code>getType();</code>
		         * </p>
		         *
		         * <p>
		         * For instance, the following code below will throw an error if
		         * there is not an instance of a DataGrid defined previously:
		         * </p>
		         *
		         * @example
		         * <listing version="3.0">
		         *
		         * var ClassReference:Class = Reflection.getType("mx.controls.DataGrid");
		         * var dataGrid:* = new ClassReference();
		         * //throws an exception since Flash Player does not contain a reference
		         * //to mx.controls.DataGrid
		         *
		         * The following will not throw an error as Flash Player has reference to
		         * mx.controls.DataGrid
		         *
		         * import mx.controls.DataGrid;
		         *
		         * var dg:DataGrid;
		         *
		         * var ClassReference:Class = Reflection.getType("mx.controls.DataGrid");
		         * var dataGrid:* = new ClassReference();
		         * //executes without error
		         *
		         * </listing>
		         *
		         * @param The String representation of the class type
		         * @throws ReflectionTypeResolveException
		         *
		         */
		public static function getType(type:String):Class {
			try {
				var ClassReference:Class=getDefinitionByName(type) as Class;
			} catch (error:Error) {
				throw new TypeError(type);
			}

			return ClassReference;
		}
	}
}